﻿console.log(hcCore);
var vm = new Vue({
    el: '#container'
    , data: {
        buttons: {}
        ,query: {
            userNo: ''
            , userName: ''
            , page: 1
            , pageSize: hcCore.pageSize
        }
        , tableData: {
            records: []
            , total: 0
        }
        , powerInfo: {
            visible: false
            , userNo: ''
            , data: []
            , value: []
        }
        , edit: {
            formId: 'editFrame'
            , title: '操作'
            , visible: false
            , fields: [{
                label: '用户账号'
                , name: 'user_no'
                , require: true
                , type: 'text'
                , disabled: false
                , value: ''
                , placeholder: '请输入账号'
                , rules: {
                    required: true, message: '账号不能为空', trigger: 'blur'
                }
            }, {
                label: '用户姓名'
                , name: 'user_name'
                , require: true
                , type: 'text'
                , disabled: false
                , value: ''
                , placeholder: '请输入姓名'
                , rules: {
                    required: true, message: '姓名不能为空', trigger: 'blur'
                }
            }, {
                label: '用户性别'
                , name: 'user_sex'
                , require: true
                , type: 'select'
                , disabled: false
                , options: [{
                    label: '男'
                    , value: '1'
                }, {
                    label: '女'
                    , value: '0'
                }]
                , value: ''
                , rules: {
                    required: true, message: '请选择性别', trigger: 'blur'
                }
            }, {
                label: '用户手机'
                , name: 'user_mobile'
                , require: true
                , type: 'text'
                , disabled: false
                , value: ''
                , placeholder: '请输入手机号码'
                , rules: [{
                    required: true, message: '手机号码不能为空', trigger: 'blur'
                }]
            }, {
                label: '用户邮箱'
                , name: 'user_email'
                , require: true
                , type: 'text'
                , disabled: false
                , value: ''
                , placeholder: '请输入常用邮箱'
                , rules: [{
                    required: true, message: '邮箱不能为空', trigger: 'blur'
                }, {
                    type: 'email', message: '请输入正确的邮箱地址', trigger: ['blur', 'change']
                }]
            }]
            , op: function () {
            }
        }
    }
    , mounted: async function () {
        const me = this;
        me.buttons = await hcCore.getPowerButton();
        if (me.buttons.hasQuery) {
            me.onQuery();
        }
    }
    , methods: {
        handleButton: function (actNo, row) {
            switch (actNo) {
                case 'Query':
                    this.onQuery();
                    break;
                case 'Add':
                    this.showEdit('add', '新增');
                    break;
                case 'Edit':
                    this.showEdit('edit', '编辑', row);
                    break;
                case 'Power':
                    this.onPower(row);
                    break;
                case 'Delete':
                    this.onDelete(row);
                    break;
                case 'ResetPassword':
                    this.onRestore(row);
                    break;
                default:
                    break;
            }
        }
        , onQuery: function () {
            let me = this;
            hcCore.post({
                url: '/SysUser/List'
                , param: {
                    user_no: me.query.userNo
                    , user_name: me.query.userName
                    , page: me.query.page
                    , page_size: me.query.pageSize
                }
                , success: function (rsp) {
                    me.tableData.records = rsp.data;
                    me.tableData.total = rsp.total;
                }
            })
        }
        , onPageChange: function (page) {
            this.query.page = page;
            this.onQuery();
        } 
        , showEdit: async function (type, title, row) {
            let me = this;
            me.resetParams(me.edit.fields);
            me.edit.title = title;
            me.edit.op = type == 'add' ? me.onInsert : (type == 'edit' ? me.onUpdate : function () { });
            if (type == 'edit') {
                if (row) {
                    await hcCore.post({
                        url: '/SysUser/Get/' + row.user_no
                        , success: function (rsp) {
                            me.edit.fields.forEach((item, index) => {
                                if (rsp.data[item.name]) {
                                    item.value = rsp.data[item.name];
                                }

                                if (item.name == 'user_no') {
                                    item.disabled = true;
                                }
                            });
                        }
                        , fault: '获得数据失败'
                    });
                }
                else {
                    hcCore.showError('请选择要编辑的记录');
                    return;
                }
            } else {

            }
            me.edit.visible = true;
        }
        , onInsert: function () {
            let me = this;
            me.$refs.edit.$refs[me.edit.formId].validate((valid) => {
                if (valid) {
                    hcCore.post({
                        url: '/SysUser/Insert'
                        , param: me.getParams()
                        , success: function (rsp) {
                            hcCore.showSuccess('新增成功');
                            me.tableData.records.push(rsp.data);
                            me.tableData.total += 1;
                            me.edit.visible = false;
                        }
                    });
                } else {
                    return false;
                }
            });
        }
        , onUpdate: function () {
            let me = this;
            me.$refs.edit.$refs[me.edit.formId].validate((valid) => {
                if (valid) {
                    hcCore.post({
                        url: '/SysUser/Update'
                        , param: me.getParams()
                        , success: function (rsp) {
                            hcCore.showSuccess('更新成功');
                            let index = me.tableData.records.findIndex(item => item.user_no == rsp.data.user_no );
                            me.$set(me.tableData.records, index, rsp.data)
                            me.edit.visible = false;
                        }
                    });
                } else {
                    return false;
                }
            });
        }
        , onDelete: function (row) {
            let me = this;
            let rows = [];
            if (row) {
                rows = [row];
            }
            else {
                rows = me.$refs.tblRecords.selection;
            }
            if (rows && rows.length > 0) {
                let tips = rows.length == 1 ? ('确认要删除【' + rows[0].user_no + '】这条记录吗') : ('确认要删除选择【' + rows.length + '】条的记录吗？');
                hcCore.confirm(tips, function () {
                    let userArr = rows.map(item => { return item.user_no });
                    let userNos = userArr.join(',');
                    hcCore.post({
                        url: '/SysUser/Delete'
                        , param: {
                            user_nos: userNos
                        }
                        , success: function (rsp) {
                            hcCore.showSuccess('删除成功');
                            userArr.forEach(item => {
                                let index = me.tableData.records.findIndex(node => item == node.user_no);
                                me.tableData.records.splice(index, 1);
                            });
                        }
                    });
                });
            }
            else {
                hcCore.showError('请选择要删除的记录');
            }
        }
        , onRestore: function (row) {
            let me = this;
            if (row) {
                hcCore.confirm('确认要还原用户【' + row.user_no + '】的密码吗？', function () {
                    hcCore.post({
                        url: '/SysUser/ResetPassword/' + row.user_no
                        , success: '还原成功'
                        , fault: '还原失败'
                    });
                });                
            }
            else {
                hcCore.showError('请选择要操作的用户');
                return;
            }
        }
        , onPower: async function (row) {
            let me = this;
            if (!row) {
                hcCore.showError('请选择要操作的用户');
                return;
            }
            me.powerInfo.userNo = row.user_no;
            await hcCore.post({
                url: '/SysRole/List'
                , param: {
                    page: 1,
                    page_size: 1000
                }
                , success: function (rsp) {
                    me.powerInfo.data = rsp.data.map(function (item) { return { key: item.role_no, label: item.role_name } });
                }
            });
            //获得用户拥有的角色信息
            await hcCore.post({
                url: '/SysUser/Roles/' + me.powerInfo.userNo
                , success: function (rsp) {
                    me.powerInfo.value = rsp.data;
                    me.powerInfo.visible = true;
                }
            });
        }
        , grantRoles: async function () {
            let me = this;
            await hcCore.post({
                url: '/SysUser/GrantRoles/' + me.powerInfo.userNo
                , param: {
                    role_nos: me.powerInfo.value
                }
                , success: function (rsp) {
                    hcCore.showSuccess('操作成功');
                    me.powerInfo.visible = false;
                }
            });
        }
        , getParams: function () {
            let param = {};
            this.edit.fields.forEach((item, index) => {
                param[item.name] = item.value;                
            });
            return param;
        }
        , resetParams: function (obj) {
            obj.forEach(item => {
                item.value = '';
                item.disabled = false;
            });
        }
        , formatterSex: function (row, column, cellValue, index) {
            if (cellValue == '1') return '男';
            else if(cellValue == '0') return '女';
            return cellValue;
        }
        , recordIndex: function (index) {
            return (this.query.page - 1) * this.query.pageSize + index + 1;
        } 
    }
});